
package com.kuyun.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.kuyun.base.base.Page;
import com.kuyun.base.mapper.BaseGroupMapper;
import com.kuyun.base.model.BaseGroup;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.model.ZtreeBean;
import com.kuyun.base.service.BaseGroupService;
import com.kuyun.base.util.Constants;
import com.kuyun.base.util.IDUtil;

/**
 * 组织机构服务接口的实现类
 * 
 * @author 王坤
 * @date 创建时间：2016年6月18日 下午8:02:46
 */
@Service("baseGroupService")
public class BaseGroupServiceImpl implements BaseGroupService {

	/**
	 * 注入组织机构mapper对象
	 */
	@Resource
	private BaseGroupMapper baseGroupMapper;

	/**
	 * @author 王坤 增加组织机构
	 * @date 2016年6月19日上午12:19:33
	 *
	 * @param baseGroup
	 * @return 0-false 1-success
	 */
	public String insert(BaseGroup baseGroup) {
		String result = "";
		baseGroup.setId(IDUtil.getId());
		baseGroup.setCreateUserId("1");
		baseGroup.setCreateTime(new Date());
		if (StringUtils.isEmpty(baseGroup.getBaseGroupParentId())) {
			baseGroup.setBaseGroupParentId("0");
			baseGroup.setBaseGroupPath("0" + baseGroup.getBaseGroupCode());
		} else {
			baseGroup.setBaseGroupPath(baseGroup.getBaseGroupParentId() + baseGroup.getBaseGroupCode());
		}
		int num = baseGroupMapper.insertGroup(baseGroup);
		if (num == 1) {
			result = ValidFormResult.Y;
		} else {
			result = ValidFormResult.N;
		}
		return result;
	}

	/**
	 * @author 王坤 查询所有组织机构
	 * @date 2016年6月19日上午12:19:33
	 *
	 * @param baseGroup
	 * @return 0-false 1-success
	 */
	public List<ZtreeBean> getGroupTree() {
		List<ZtreeBean> list = baseGroupMapper.getGroupTree();
		return list;
	}

	/**
	 * @author 王坤 更新组织机构
	 * @date 2016年6月19日下午7:55:27
	 *
	 * @param baseGroup
	 * @return
	 */
	public String update(BaseGroup baseGroup) {
		String result = "";
		baseGroup.setUpdateUserId("22");
		if (StringUtils.isEmpty(baseGroup.getBaseGroupParentId())) {
			baseGroup.setBaseGroupParentId("0");
			baseGroup.setBaseGroupPath("0" + baseGroup.getBaseGroupCode());
		} else {
			baseGroup.setBaseGroupPath(baseGroup.getBaseGroupParentId() + baseGroup.getBaseGroupCode());
		}
		baseGroup.setUpdateTime(new Date());
		int num = baseGroupMapper.updateGroup(baseGroup);
		if (num == 1) {
			result = ValidFormResult.Y;
		} else {
			result = ValidFormResult.N;
		}
		return result;
	}

	/**
	 * 根据CODE查询组织机构
	 * 
	 * @author 唐靖
	 *
	 * @date 2016年6月25日下午2:39:31
	 *
	 * @param dicPrivilegeTypeMenu
	 * @return
	 */
	public BaseGroup findByCode(String dicPrivilegeTypeMenu) {

		return baseGroupMapper.findByCode(dicPrivilegeTypeMenu);
	}

	/**
	 * @author 王坤
	 *
	 * @date 2016年6月29日下午10:18:46
	 *
	 * @return
	 */
	public Page<BaseGroup> findByParentCode(Page<BaseGroup> page, String parentId, String search) {
		int num = baseGroupMapper.findByParentCodeCount("root".equals(parentId) ? "0" : parentId, null);
		if (num == 0) {
			return page;
		}
		page.setTotal(num);
		List<BaseGroup> list = baseGroupMapper.findByParentCode(page.getPageSize(), (page.getPageNumber() - 1)*page.getPageSize(),
				"root".equals(parentId) ? "0" : parentId, search);
		page.setRows(list);
		return page;
	}

	/**
	 * 根据Parent查询组织机构总量
	 */
	public Integer findByParentCodeCount(String parentId, String search) {
		return baseGroupMapper.findByParentCodeCount(parentId, search);
	}

	/**
	 * 根据ID查询组织机构
	 */
	public BaseGroup findByID(String id) {
		return baseGroupMapper.findById(id);
	}

	/**
	 * 删除组织机构
	 */
	public ValidFormResult deleteGroup(String id) {
		ValidFormResult result = new ValidFormResult();
		int count = baseGroupMapper.findSonGroup(id);
		if (count > 0) {
			result.setStatus(ValidFormResult.N);
			result.setInfo("存在子节点，不支持删除！");
		} else {
			baseGroupMapper.deleteGroup(id);
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}

	/**
	 * 判断code是否存在
	 */
	public boolean checkCode(BaseGroup baseGroup, String id) {
		int num = baseGroupMapper.checkCode(baseGroup);
		if (id != null) {
			BaseGroup basedic = baseGroupMapper.findById(id);
			if (basedic.getBaseGroupCode().equals(baseGroup.getBaseGroupCode())) {
				return true;
			}
		}
		if (num > 0) {
			return false;
		}
		return true;
	}

	/**
	 * 保存用户组织机构
	 * 
	 * @author 唐靖
	 *
	 * @date 2017年7月4日下午9:40:58
	 *
	 * @param ids
	 * @param id
	 */
	public void saveUserGroup(String ids, String id) {
		baseGroupMapper.deleteUserGroup(id);
		String[] groupids = ids.split(",");
		List<Map<String, String>> params = new ArrayList<Map<String, String>>();
		for (int i = 0; i < groupids.length; i++) {
			Map<String, String> p = new HashMap<String, String>();
			p.put("id", IDUtil.getId());
			p.put("userid", id);
			p.put("groupid", groupids[i]);
			params.add(p);
		}
		baseGroupMapper.saveUserGroup(params);

	}

	/**
	 * 为了用户选择而生成的tree
	 * 
	 * @author 唐靖
	 *
	 * @date 2017年7月4日下午9:52:56
	 *
	 * @param userId
	 * @return
	 */
	public List<ZtreeBean> getBaseGroupTreeForUser(String userId) {

		return baseGroupMapper.getBaseGroupTreeForUser(userId);
	}

}
